# -*-encoding: utf-8 -*-

from pyramid.view import view_config
from pyramid.view import view_defaults
from pyramid.response import Response
from pyramid.httpexceptions import HTTPFound
import time, os, uuid, shutil, hashlib, base64, json, random
import logging
from ..utils.Queue.phd.sqlwriter import commit


from ..utils.Queue import UploadTasks
from ..utils.Hash.HashUtil import HashUtil
from ..utils.Image.ImageUtil import ImageUtil
from ..utils.DB.MySQL import MySqlCRUD
from ..utils.SMS import MobSMS
from ..utils.Cache.TokenCache import TokenCache
from ..utils.Cache.ChecksumCache import ChecksumCache
from ..utils.DB.PostgreSQL import PostgreCRUD

import sys
reload(sys)
sys.setdefaultencoding('utf-8')
log = logging.getLogger(__name__)

class CeleryIndex:
    
    def __init__(self, request):
        self.request = request
    
    #@view_config(route_name='AppWebRedirectRoute', request_method='GET')
    #def AppWebRedirectRoute(self):
    #    url = self.request.params['url']
    #    if url.startswith("http"):
    #        pass
    #    else:
    #        url = "http://" + url
    #    return HTTPFound(location=url);
            
    #    username = self.request.session['username']
    #    return HTTPFound(location='/')


    #    postParams =  self.request.POST
    #    tel = postParams.getone('tel')
    #    tString = postParams.getone('tString')
    #    eString = postParams.getone('eString')
    #    tokenString = postParams.getone('tokenString')
    #    csc = postParams.getone('csc')

    @view_config(route_name='CeleryGetRoute', request_method='GET')
    def CeleryGetRoute(self):
        a = random.randint(0, 9)
        b = random.randint(0, 9)
        map = {'tel': '18500936652', 'username': 'lczgywzyy', 'email': 'xyq547133@163.com', 'a': a, 'b': b, 'a+b': (a + b)}
        jsonStr = json.dumps(map, ensure_ascii=False)
        log.debug('[CELERY][GET][JSONSTR]' + jsonStr)
        commit.delay(jsonStr)
        return Response(jsonStr)

    @view_config(route_name='CeleryPostRoute', request_method='POST')
    def CeleryPostRoute(self):
        map = {'RetCode': '0', 'message':"Community URL", 'data':'http://tieba.baidu.com/f?kw=%E5%A4%9A%E5%AE%9D%E4%B8%B2%E8%81%94%E7%9B%9F&qq-pf-to=pcqq.c2c'}
        jsonStr = json.dumps(map, ensure_ascii=False)
        log.debug('[CELERY][POST][JSONSTR]' + jsonStr)
        return Response(jsonStr)
